package com.student.dms;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter4.BaseQuickAdapter;
import com.chad.library.adapter4.QuickAdapterHelper;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.luck.picture.lib.utils.ToastUtils;
import com.student.db.AccountDao;
import com.student.db.AccountEntity;
import com.student.db.AppDatabase;
import com.student.db.PushDao;
import com.student.db.PushEntity;
import com.student.dms.databinding.ActivityAnnouncementPushBinding;
import com.student.dms.databinding.ActivityLoginBinding;
import com.student.dms.databinding.ItemPushBinding;
import com.student.dms.databinding.ItemRecyerviewBinding;

import java.io.ByteArrayOutputStream;
import java.io.File;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;
import kotlin.jvm.functions.Function1;

/**
 *  公告展示
 * */
public class AnnouncementPushActivity extends BaseActivity {
    private ActivityAnnouncementPushBinding activityAnnouncementPushBinding;
    private BaseQuickAdapter baseQuickAdapter;
    private AppDatabase roomDatabase;
    private AccountEntity accountEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        roomDatabase = DMSApplication.getApplication().getRoomDatabase();
        activityAnnouncementPushBinding = ActivityAnnouncementPushBinding.inflate(getLayoutInflater());
        setContentView(activityAnnouncementPushBinding.getRoot());
        activityAnnouncementPushBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(TitleBar titleBar) {
                finish();
            }
        });
        activityAnnouncementPushBinding.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(AnnouncementPushActivity.this, ActivityEditPush.class));
            }
        });
        SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        String username = sharedPreferences.getString("username", "");
        if (!TextUtils.isEmpty(username)) {

            AccountDao accountDao = roomDatabase.provideAccountDao();
            Single<AccountEntity> accountFlow = accountDao.searchAccountByName(username);

            disposable.add(accountFlow.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(accountEntity -> {
                this.accountEntity =accountEntity;
                if (accountEntity.getType() == 1) {
                    activityAnnouncementPushBinding.add.setVisibility(View.VISIBLE);
                } else {
                    activityAnnouncementPushBinding.add.setVisibility(View.GONE);
                }
            }, studentFlowThrowable -> {
                ToastUtils.showToast(AnnouncementPushActivity.this, "用户不存在，请注册！");
            }));
        }


        baseQuickAdapter = new BaseQuickAdapter<PushEntity, VH>() {
            @NonNull
            @Override
            protected VH onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
                VH vh = new VH(ItemPushBinding.inflate(LayoutInflater.from(context), viewGroup, false));
                return vh;
            }

            @Override
            protected void onBindViewHolder(@NonNull VH vh, int position, @Nullable PushEntity itemData) {
                String imagePath = itemData.getImagePath();
                File file = new File(imagePath);
                if(file.exists()){
                    Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
                    vh.itemViewBidnng.imageView.setImageBitmap(bitmap);
                }

                vh.itemViewBidnng.title.setText("标题："+itemData.getTitle());
                vh.itemViewBidnng.content.setText("内容："+itemData.getContent());

                vh.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (accountEntity.getType() == 1) {
                            Intent intent = new Intent(AnnouncementPushActivity.this, ActivityEditPush.class);
                            intent.putExtra("pushData",itemData);
                            startActivity(intent);
                        }
                    }
                });
            }
        };
        QuickAdapterHelper build = new QuickAdapterHelper.Builder(baseQuickAdapter).build();
        activityAnnouncementPushBinding.recyclerView.setAdapter(build.getAdapter());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(AnnouncementPushActivity.this);
        activityAnnouncementPushBinding.recyclerView.setLayoutManager(linearLayoutManager);

    }


    @Override
    protected void onResume() {
        super.onResume();
        //查询数据库更新数据
        PushDao pushDao = roomDatabase.providePushDao();
        disposable.add(pushDao.getAllPush().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(list -> {
            baseQuickAdapter.submitList(list);
        }));
    }

    static class VH extends RecyclerView.ViewHolder {
        @NonNull
        public ItemPushBinding itemViewBidnng;

        public VH(@NonNull ItemPushBinding itemViewBidnng) {
            super(itemViewBidnng.getRoot());
            this.itemViewBidnng = itemViewBidnng;
        }
    }

}
